package com.shundr.auth.common.util;

import android.R.interpolator;
import android.content.Context;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.GeofenceClient;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.location.LocationClientOption.LocationMode;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.search.core.SearchResult;
import com.baidu.mapapi.search.geocode.GeoCodeResult;
import com.baidu.mapapi.search.geocode.GeoCoder;
import com.baidu.mapapi.search.geocode.OnGetGeoCoderResultListener;
import com.baidu.mapapi.search.geocode.ReverseGeoCodeOption;
import com.baidu.mapapi.search.geocode.ReverseGeoCodeResult;
import com.baidu.mapapi.search.geocode.ReverseGeoCodeResult.AddressComponent;
import com.shundr.auth.common.model.AreaInfo;
import com.shundr.auth.frame.util.LLoader;
import com.shundr.auth.frame.util.LLogger;
import com.shundr.auth.frame.util.ToastHelper;

public class BDLocManager implements BDLocationListener, OnGetGeoCoderResultListener {
	
	private static LocationClient client;
	private GeoCoder geoCoder;
	private Context context;
	private LocationInfo info = new LocationInfo();
	
	private GetLocaionListener listener = null;
	
	public void init(Context context) {
		
		this.context = context;
		
		client = new LocationClient(context);
		LocationClientOption option = new LocationClientOption();
		option.setLocationMode(LocationMode.Hight_Accuracy);//设置定位模式
		option.setCoorType("bd09ll");//返回的定位结果是百度经纬度,默认值gcj02
//		option.setScanSpan(5000);//设置发起定位请求的间隔时间为5000ms
		option.setIsNeedAddress(true);//返回的定位结果包含地址信息
		option.setNeedDeviceDirect(true);//返回的定位结果包含手机机头的方向
		client.setLocOption(option);
		client.registerLocationListener(this);
		
		geoCoder = GeoCoder.newInstance();
		geoCoder.setOnGetGeoCodeResultListener(this);
	}
	
	public boolean hasStart() {
		return client != null && client.isStarted();
	}
	
	public void start() {
		client.start();
	}
	
	public void stop() {
		client.stop();
	}
	
	public void requestLocation() {
		client.requestLocation();
	}
	
	public void setGetLocaionListener(GetLocaionListener listener) {
		this.listener = listener;
	}

	@Override
	public void onReceiveLocation(BDLocation location) {
		LLogger.e("locType:" + location.getLocType() + " lat:" + location.getLatitude() + " lon:" + location.getLongitude());
		switch (location.getLocType()) {
		case 61:	//GPS
		case 66:	// off line
		case 161:	// net location
			info.Latitude = location.getLatitude();
			info.Longitude = location.getLongitude();
			
			if (info.Latitude < 1 || info.Longitude < 1) {
				requestLocation();
				return;
			}
			
			if (listener != null) {
				listener.getLocationInfo(info);
			}
			ReverseGeoCodeOption option = new ReverseGeoCodeOption();
			option.location(new LatLng(info.Latitude, info.Longitude));
			
			geoCoder.reverseGeoCode(option);
			break;
			
		default:
			requestLocation();// request failed; try again
			break;
		}
		
	}

	@Override
	public void onGetGeoCodeResult(GeoCodeResult result) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onGetReverseGeoCodeResult(ReverseGeoCodeResult result) {
		if (result == null || result.error != SearchResult.ERRORNO.NO_ERROR) {
			ToastHelper.toastShort(context, "解析经纬度失败！");
		} else {
			AddressComponent component = result.getAddressDetail();
			AreaInfo areaInfo = AreaUtils.getAreaInfo(component.province, component.city, component.district);
			info.address = result.getAddress();
			info.addrCode = areaInfo.getCode();
			LLogger.e("addr:" + result.getAddress() + " addrCode:" + areaInfo.getCode());
			if (listener != null) {
				listener.getLocationInfo(info);
			}
		}
		
	}

	public static class LocationInfo {
		public double Latitude = 0;
		public double Longitude = 0;
		public String address = "";
		public String addrCode = "";
	}
	
	public interface GetLocaionListener {
		public void getLocationInfo(LocationInfo info);
	}
}
